package com.chuangjiangx.member.basic.ddd.application;

import com.alibaba.fastjson.JSON;
import com.chuangjiangx.commons.sms.CodeMsg;
import com.chuangjiangx.commons.sms.SMSUtils;
import com.chuangjiangx.member.basic.ddd.application.command.MobileMsgCommand;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author cj-ch
 * @date 2018/9/14 下午3:27
 */
@Slf4j
@Service
public class MbrSmsApplication {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    public static final String REDIS_RONGLIAN_SFYZ_SMS = "ronglian_sfyz_sms";

    /**
     * 发送手机验证码
     * @param mobile
     */
    public String sendMobileVerifyCode(String mobile){
        String code = null;
        try {
            code = SMSUtils.sendCodeInfo(mobile, redisTemplate);
        } catch (Exception e) {
            e.printStackTrace();
        }
        CodeMsg codeMsg = new CodeMsg(code);
        log.info("手机:{},验证码:{}",mobile,code);

        MobileMsgCommand mobileMsgCommand = new MobileMsgCommand();
        mobileMsgCommand.setType(2);
        mobileMsgCommand.setRec_num(mobile);
        mobileMsgCommand.setMessage(new String[]{code});
        mobileMsgCommand.setSms_param(JSON.toJSONString(codeMsg));

        //添加发送短信数据
        redisTemplate.opsForList().rightPush(REDIS_RONGLIAN_SFYZ_SMS, JSON.toJSONString(mobileMsgCommand));
        //保存验证码到redis
        SMSUtils.saveCodeInfo(mobile, code, redisTemplate);
        return code;
    }
}
